package com.yami.shop.search.common.manager;

import cn.hutool.core.collection.CollectionUtil;
import com.yami.shop.bean.param.EsProductParam;
import com.yami.shop.bean.vo.search.ProductSearchVO;
import com.yami.shop.common.constants.EsCacheNames;
import com.yami.shop.common.enums.EsRenovationProductSortEnum;
import com.yami.shop.common.util.EsPageVO;
import com.yami.shop.common.util.RedisUtil;
import com.yami.shop.search.common.param.EsPageParam;
import com.yami.shop.search.common.service.SearchProductService;
import com.yami.shop.service.OrderItemService;
import com.yami.shop.service.ProdCommService;
import com.yami.shop.common.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * @author FrozenWatermelon
 * @date 2020/11/16
 */
@Component
public class ProductSearchManager {


    @Autowired
    private ProdCommService prodCommService;
    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private SearchProductService searchProductService;

    public void addRenovationSpuCache(String key, EsProductParam productSearchDTO, Long shopId, Integer expireTime) {
        if(Objects.isNull(productSearchDTO.getEsTimeRange()) && Objects.isNull(productSearchDTO.getEsRenovationSpuSort())){
            return;
        }
        // 计算时间
        int dayNum = 0;
        switch (productSearchDTO.getEsTimeRange()){
            case 1:
                dayNum = 365;
                break;
            case 2:
                dayNum = 90;
                break;
            case 3:
                dayNum = 30;
                break;
            case 4:
                dayNum = 7;
                break;
            default:
                break;
        }
        List<Long> spuIds;
        // 1.获取根据条件的商品ids
        if(Objects.equals(productSearchDTO.getEsRenovationSpuSort(), EsRenovationProductSortEnum.COMMENT_NUM_DESC.value()) || Objects.equals(productSearchDTO.getEsRenovationSpuSort(),EsRenovationProductSortEnum.COMMENT_NUM_ASC.value())){
            // 获取时间范围内的评论数排序,放入redis
            spuIds = prodCommService.getCommNumRankSpuIdsByShopIdAndTime(key, shopId, dayNum, expireTime,productSearchDTO.getEsRenovationSpuSort());
        }else {
            // 获取时间范围内的销量排序，放入redis
            spuIds = orderItemService.getSoldNumRankByShopIdAndTime(key, shopId, dayNum, expireTime, productSearchDTO.getEsRenovationSpuSort());
        }
        // 2.判断数量是否为1000个，没有就按创建时间排序补充剩余
        if(spuIds.size() >= EsCacheNames.RENOVATION_PRODUCT_IDS_LIMIT){
            return;
        }

        // 不足1000的部分直接查询es数据存入redis
        int surplusNum = EsCacheNames.RENOVATION_PRODUCT_IDS_LIMIT - spuIds.size();
        EsPageParam pageDTO = new EsPageParam();
        pageDTO.setCurrent(1);
        pageDTO.setSize(surplusNum);
        EsProductParam productParam = BeanUtil.map(productSearchDTO, EsProductParam.class);
        productParam.setSpuIdsExclude(spuIds);
        productParam.setShowSpuType(0);
        EsPageVO<ProductSearchVO> searchPage = searchProductService.renovationPage(pageDTO, productParam, null);
        if (CollectionUtil.isNotEmpty(searchPage.getRecords())) {
            List<Long> esSpuIds = searchPage.getRecords().stream().map(ProductSearchVO::getProdId).collect(Collectors.toList());
            spuIds.addAll(esSpuIds);
        }
        // 放入es缓存
        RedisUtil.setRightPushAll(key,spuIds,expireTime);
    }
}
